package com.gz.furnitures.dao.impl;

import com.gz.furnitures.dao.BasicDao;
import com.gz.furnitures.dao.OrderDao;
import com.gz.furnitures.entity.Furn;
import com.gz.furnitures.entity.Order;
import com.gz.furnitures.entity.OrderItem;

import java.util.Collections;
import java.util.List;

public class OrderDaoImpl extends BasicDao<Order> implements OrderDao {
    @Override
    public int saveOrder(Order order) {
        //这里的id就是订单号
        String sql = "INSERT into `order`(`id`,`create_time`,`price`,`status`,member_id)" +
                " VALUES(?,?,?,?,?)";
        return update(sql,order.getId(),order.getCreateTime(),
                order.getPrice(),order.getStatus(),order.getMemeberId());
    }

    @Override
    public List<Order> queryOrder() {
        String sql = "SELECT `id`,`create_time` createTime,`price`,`status`,member_id memeberId FROM `order`";
        return queryMulti(sql,Order.class);
    }

    @Override
    public int getTotalRowByMemberId(int memberId) {
        String sql="select count(*) from `order` where member_id =?";
        //注意，这里不能写成return (Integer)queryScalar(sql);
        // 他返回的不是Integer类型，会报cast异常
        return ((Number)queryScalar(sql,memberId)).intValue();
    }

    @Override
    public List<Order> getOrdersByMemberId(int begin, int pageSize, int memberId) {
        String sql = "SELECT `id`,`create_time` createTime,`price`,`status`,member_id memeberId FROM `order` WHERE `member_id` = ? LIMIT ?,?";
        return queryMulti(sql,Order.class,memberId,begin,pageSize);
    }

    public List<Order> getPageOrders(int begin, int pageSize) {
        String sql="select `id`,`create_time` createTime,`price`,`status`,member_id memeberId from `order`  limit ?,?";
        return queryMulti(sql, Order.class,begin,pageSize);
    }
}
